/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.tags;

import com.apache.api.vo.ResultEntity;
import com.apache.portal.common.oscache.BaseOsCache;
import com.apache.portal.common.oscache.OsCacheOtherManager;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.ConfigUtil;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  获取站点对象tag信息
 *
 * @author 创建时间：2017-6-24
 */
@SuppressWarnings("serial")
public class HostTag extends TagSupport {

    private String siteCode;

    public int doStartTag() throws JspException {
        try {
            HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
            Map<String, Object> params = new HashMap<String, Object>();
            String hostName = request.getServerName();//request.getLocalName();

            BaseOsCache oscache = OsCacheOtherManager.getInstance()
                    .getBaseOsCache("sys_website_data_", 60 * 60 * 24 * 30);
            Map website = new HashMap();
            if (!StrUtil.isNull(siteCode)) {
                website = (Map) oscache.get(siteCode);
                if (website == null) {
                    website = initWebSite(siteCode, null);
                    if (website == null) {
                        website = new HashMap();
                        website.put("sysName", "nyt");
                        website.put("siteCode", "nyt");
                        website.put("indexTemp", "");
                    }
                }
            } else {
                website = (Map) oscache.get(hostName);
                if (website == null) {
                    website = initWebSite(null, hostName);
                    if (website == null) {
                        website = new HashMap();
                        website.put("sysName", "nyt");
                        website.put("siteCode", "nyt");
                        website.put("indexTemp", "");
                    }
                }
            }
            request.setAttribute("website", website);
        } catch (Exception e) {
            e.printStackTrace();
            return super.SKIP_BODY;
        }
        return super.EVAL_BODY_INCLUDE;
    }

    private InterfaceRegister getInterfaceRegister() {
        InterfaceRegister register = new InterfaceRegister();
        String reqUrl = ConfigUtil.getInstance().getValueByKey("info_release_url");
        String infoUrl = reqUrl + "/wcm/service/doService.action";
        if (reqUrl.startsWith("https:")) {
            register.setCallType("https");
            register.setAddress(infoUrl);
        } else if (reqUrl.startsWith("http:")) {
            register.setCallType("http");
            register.setAddress(infoUrl);
        } else {
            String port = ConfigUtil.getInstance().getValueByKey("info_release_port");
            register.setAddress(reqUrl);
            register.setPort(port);
            register.setCallType("socket");
        }
        return register;
    }

    private Map initWebSite(String siteCode, String siteHost) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("modelTypes", "s_tWebsite");
        param.put("resultType", "objInfo");
        param.put("sqlKeyId", "objInfo");
        param.put("sysPass", ConfigUtil.getInstance().interfacePass());
        ResultEntity entity = LoadRpcService.service()
                .doServiceClient(PortalPubFactory.getInstance().getBeanId("cms"), "dymicSql", param,
                        PortalPubFactory.getInstance().getRpcInfo("wcm"));
        Map result = null;
        if (!ToolsUtil.isEmpty(entity.getEntity())) {
            List list = (List) entity.getEntity();
            BaseOsCache oscache = OsCacheOtherManager.getInstance()
                    .getBaseOsCache("sys_website_data_", 60 * 60 * 24 * 30);
            for (int i = 0; i < list.size(); i++) {
                Map map_siteHost = (Map) list.get(i);
                String key_siteHost = String.valueOf(map_siteHost.get("siteHost"));
                if (StrUtil.isNotNull(key_siteHost)) {
                    if (StrUtil.isNotNull(siteHost) && key_siteHost.equalsIgnoreCase(siteHost)) {
                        result = map_siteHost;
                        break;
                    }
                    oscache.put(key_siteHost, map_siteHost);
                }

                Map map_siteCode = (Map) list.get(i);
                String key_siteCode = String.valueOf(map_siteCode.get("siteCode"));
                if (StrUtil.isNotNull(key_siteCode)) {
                    if (StrUtil.isNotNull(siteCode) && key_siteCode.equalsIgnoreCase(siteCode)) {
                        result = map_siteCode;
                        break;
                    }
                    oscache.put(key_siteCode, map_siteCode);
                }
            }
        }
        return result;
    }

    public String getSiteCode() {
        return siteCode;
    }

    public void setSiteCode(String siteCode) {
        this.siteCode = siteCode;
    }

}
